<?php

class ModelSaleOrderBasket extends Model {
	public function getBaskets($filter, &$total) {
		$sql = "SELECT ob.* FROM  @@order_basket ob";
		
		if ($filter->get('filter_order_id')) {
			$sql .= " LEFT JOIN @@order o ON ob.order_basket_id = o.order_basket_id WHERE o.order_id = {$filter->get('filter_order_id')}";
		}		
		else {
			$sql .= " WHERE 1";
		}
		
		if ($filter->get('filter_basket_id')) {
			$sql .= " AND ob.order_basket_id={$filter->get('filter_basket_id')}";
		}
		
		$sql .= $filter->sort('order_basket_id', 'DESC');
		$sql .= $filter->limit();
		
		$total = $this->db->queryCount($sql);
				
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function addOrderBasket($filter) {
		
		$order_basket_id = $this->db->queryOne("SELECT max(order_basket_id) FROM @@order_basket");
		if (!$order_basket_id) $order_basket_id = 1;
		else $order_basket_id++;
		
		$sql = '';
		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(date_added) >= '".ES($filter->get('filter_date_start'))."'";
		}
		
		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(date_added) <= '".ES($filter->get('filter_date_end'))."'";
		}
		
		if ($filter->get('filter_order_status_id') !== '*'){
			$sql .= " AND order_status_id = ".(int) $filter->get('filter_order_status_id');
		}
		
		$sql = "UPDATE `@@order` SET order_basket_id = $order_basket_id WHERE order_basket_id = 0 $sql";
		$this->db->runSql($sql);
			
		$sql = "SELECT count(DISTINCT model) AS skus, count(DISTINCT op.order_id) AS orders FROM `@@order` o,  @@order_product op
			WHERE o.order_basket_id = $order_basket_id AND o.order_id = op.order_id";
		$result = $this->db->queryOne($sql);
		
		$data = array(
			'order_basket_id' => $order_basket_id,
			'total_orders' => $result['orders'],
			'order_rate' => 0,
			'total_skus' => $result['skus'],
			'date_added' => $GLOBALS['V5_NOW'],
			'style' => 0,
			'note' => '',
			'status' => 0
		);
		$this->db->insert('order_basket', $data);
	}
	
	function deleteOrder($order_basket_id, $order_id) {
		if (is_array($order_id)) $order_id = implode(',', $order_id);
		$this->db->runSql("UPDATE `@@order` SET order_basket_id = 0 WHERE order_basket_id= $order_basket_id AND order_id IN ($order_id)");
		
		$sql = "SELECT count(DISTINCT model) AS skus, count(DISTINCT op.order_id) AS orders FROM `@@order` o,  @@order_product op
				WHERE order_basket_id = $order_basket_id AND o.order_id = op.order_id";
		$result = $this->db->queryOne($sql);
		
		return $this->db->runSql("UPDATE  @@order_basket SET total_orders='{$result['orders']}', total_skus='{$result['skus']}' WHERE order_basket_id = $order_basket_id");
	}
	
	function addOrder($order_basket_id, $order_id) {
		if (is_array($order_id)) $order_id = implode(',', $order_id);
		$this->db->runSql("UPDATE `@@order` SET order_basket_id = $order_basket_id WHERE order_basket_id= 0 AND order_id IN ($order_id)");
		
		$sql = "SELECT count(DISTINCT model) AS skus, count(DISTINCT op.order_id) AS orders FROM `@@order` o,  @@order_product op
				WHERE order_basket_id = $order_basket_id AND o.order_id = op.order_id";
		$result = $this->db->queryOne($sql);
		
		return $this->db->runSql("UPDATE  @@order_basket SET total_orders='{$result['orders']}', total_skus='{$result['skus']}' WHERE order_basket_id = $order_basket_id");
	}
	
	function getSKUs($order_basket_id) {		
		$sql = "SELECT op.product_id, op.model, op.name, p.quantity as stock, p.image, p.price, p.cost, sum(op.quantity) as qty, p.sku, p.supplier_id
			FROM `@@order_product` op, `@@order` o, `@@product` p WHERE op.product_id = p.product_id AND op.order_id = o.order_id  AND o.order_basket_id = $order_basket_id";
		$sql .= " GROUP BY op.product_id ORDER BY supplier_id";
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	function getOrderBasket($order_basket_id) {
		return $this->db->queryOne("SELECT * FROM  @@order_basket WHERE order_basket_id = $order_basket_id");
	}
	
	function deleteOrderBasket($order_basket_id) {
		$this->db->runSql("UPDATE @@order SET order_basket_id = 0 WHERE order_basket_id = $order_basket_id");
		return $this->db->runSql("DELETE FROM @@order_basket WHERE order_basket_id = $order_basket_id");
	}
}